<template>
	<view class="logbox">
		<u-popup :show="show" @close="close" :round="20">
			<view class="mainpadding">
				<!-- <view class="flexcenter">
					<view class="margin_right1">
						<u-icon name="weixin-fill" color="#09BB07" size="28"></u-icon>
					</view>
					<view class="xiaohei ershiba">微信登录</view>
				</view> -->
				<view class="flexcenter margin_top4" @click="yzmdl('/pages/login/yanzhengmdl?type=',typevalue)">
					<view class="margin_right1">
						<image class="sjyz" src="../../static/image/system/sj.png" mode=""></image>
					</view>
					<view class="xiaohei ershiba">手机验证码登录</view>
				</view>
				<view class="bigbtn margin_top5 flexcenter" @click="close">取消</view>
			</view>
		</u-popup>
		<view class="xiaohei fonweight" style="font-size: 56rpx;">登录</view>
		<view class="bigtext xiaohui margin_top5">还没有账号？立即
			<text class="bigtext xiaolan fonweight" @click="jumper('/pages/login/zhuce')">注册</text>
		</view>
		<view class="xiahuaxian margin_top6">
			<input type="number" placeholder="请输入手机号" v-model="mobile">
		</view>
		<!-- <view class="xiahuaxian margin_top">
			<input type="password" placeholder="请输入请输入密码" v-model="password">
		</view> -->
		<view class="xiahuaxian margin_top flexbetween">
			<input type="number" placeholder="请输入手机验证码" v-model="yzm_code">
			<view class="strongtext xiaolan textcenter" style="width: 240rpx;" @click="sendyzm" v-show="codetext == 1">
				获取验证码</view>
			<view class="strongtext xiaolan textcenter" style="width: 240rpx;" v-show="codetext == 2">{{second}}s后重新获取
			</view>
			<view class="strongtext xiaolan textcenter" style="width: 240rpx;" @click="sendyzm" v-show="codetext == 3">
				重新发送</view>
		</view>
		<!-- <view class="flexbetween margin_top">
			<view class="strongtext xiaohui" @click="jumper('/pages/login/wangjmm')">忘记密码？</view>
			<view class="strongtext xiaolan" @click="show = true">其他方式登录</view>
		</view> -->
		<!-- 未输入展示的按钮 -->
		<view class="margin_top6 denglhbtn flexcenter" @click="login()">登录</view>
		<view class="margin_top denglhbtns flexcenter" @click="shouye">暂不登录</view>
		<!-- 已输入展示的按钮 -->
		<!-- <view class="margin_top6 denglhbtn flexcenter">登录</view> -->
		<view class="gudingdb ffffff mainpadding" @click="type = !type">
			<view class="flexcenter">
				<!-- 已选中 -->
				<view class="margin_right1" v-show="type">
					<u-icon name="checkmark-circle-fill" color="#2979ff" size="16"></u-icon>
				</view>
				<!-- 为选中 -->
				<view class="weixz margin_right1" v-show="!type"></view>
				<view class="flexleft">
					<view class="strongtext xiaohui">我已阅读并同意本平台</view>
					<view class="strongtext xiaolan fonweight" @click.stop="jumper('/pages/system/system?type=1')">用户协议
					</view>
					<view class="strongtext xiaohui">和</view>
					<view class="strongtext xiaolan fonweight" @click.stop="jumper('/pages/system/system?type=2')">隐私协议
					</view>
				</view>
			</view>
		</view>
	</view>

</template>

<script>
	import WebSocket from '@/common/websocket.js' //websocket封装
	const httpRequest = require("@/common/httpRequest.js")
	// #ifdef APP-PLUS
	const TUICallKit = uni.requireNativePlugin('TencentCloud-TUICallKit');
	// #endif
	export default {
		data() {
			return {
				show: false,
				mobile: "",
				type: false,
				password: "",
				liandiantype: 0,
				typevalue: 0,
				websocket: null,
				codetext: 1,
				second: 59,
				yzm_code:""
			}
		},
		onLoad(options) {
			if (options.type) {
				this.typevalue = options.type
			}
		},
		methods: {
			isMobile(value) {
				let a = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value);
				return a;
			},
			sendyzm() {
				let _this = this;
				if (_this.mobile == '') {
					httpRequest.toast("请输入手机号码!")
					return;
				}
				if (!_this.isMobile(_this.mobile)) {
					httpRequest.toast("手机号码格式错误!")
					return;
				}
				_this.codetext = 2;
				_this.second = 59;
				let timer = setInterval(function() {
					if (_this.second > 1) {
						_this.second--;
					} else {
						clearInterval(timer);
						_this.codetext = 3;
					}
				}, 1000)
				httpRequest.request('/api/sms/send', 'POST', {
					mobile: this.mobile,
					event: 'mobilelogin',
				}, false, true, false).then(res => {
					if (res.code == 1) {
						httpRequest.toast('发送成功')
					}
				})
			},
			websocketStart() {
				this.websocket = new WebSocket("wss://kuaixun.yongshunqiyuewangluo.com:8282");
				this.websocket.connect()
				this.websocket.onMessage(result => {
					console.log('app.js收到服务器内容：' + result.data);
					uni.$emit('updatechatFnte', "")
					uni.$emit('updatechatFnteList', "")
				})
			},
			shouye() {
				uni.reLaunch({
					url: "/pages/homepage"
				})
			},
			//验证手机号
			isMobile(value) {
				let a = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value);
				return a;
			},
			login() {
				let _this = this;
				if(_this.mobile == ''){
					httpRequest.toast("请输入手机号码!")
					return;
				}
				if(!_this.isMobile(_this.mobile)){
					httpRequest.toast("手机号码格式错误!")
					return;
				}
				if(this.yzm_code==""){
					httpRequest.toast("请输入验证码")
					return false
				} 
				if(!this.type){
					httpRequest.toast("请认真阅读并勾选用户协议");
					return false;
				}
				this.liandiantype = 1
				uni.showLoading({
					mask: true,
					title: "请稍候"
				})
				httpRequest.request('/api/user/mobilelogin','GET',{
					mobile:this.mobile, 
					captcha:this.yzm_code
				}).then(res => {
					if (res.code == 1) {
						uni.clearStorageSync();
						uni.hideLoading()
						httpRequest.toast('登录成功');
						// this.$store.dispatch('connectSocket', res.data.userinfo.token)
						this.websocketStart();
						uni.setStorageSync('token', res.data.userinfo.token);
						uni.setStorageSync('invite_code', res.data.userinfo.invite_code);
						uni.setStorageSync('user_id', res.data.userinfo.user_id);
						this.userinfofn()
						// httpRequest.request('/api/user/profile', 'GET', {online:1}).then(res => {})
						setTimeout(() => {
							if (this.typevalue == 1) { //退出登录进来的这个页面
								uni.reLaunch({
									url: "/pages/homepage"
								})
							} else {
								uni.navigateBack(1)
							}
						}, 1000)
					} else {
						this.liandiantype = 0
						uni.hideLoading()
						httpRequest.toast(res.msg);
					}
				})
			},
			userinfofn() {
				httpRequest.request('/api/user/getUserSig', 'GET', {}).then(res => {
					let user_id = uni.getStorageSync("user_id")
					this.tuicallkitLogin(user_id,res.data.userSig)
				})
			},
			tuicallkitLogin(userId, userSig) {
				console.log(userId, userSig, "555555555555555555555555555")
				const options = {
					SDKAppID: 1600021546, // 请替换为步骤一取到的 SDKAppID
					userID: userId.toString(), // 请替换为您的 UserID
					userSig: userSig.toString(), // 您可以在控制台中计算一个 UserSig 并填在这个位置
				};
				
				TUICallKit.login(options, (res) => {
					if (res.code === 0) {
						console.log('login success》》》》》》腾讯云音视频连接成功');
					} else {
						console.log(`login failed, error message = ${res.msg}`);
					}
				});
			},
			isMobile(value) {
				let a = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value);
				return a;
			},

			close() {
				this.show = false
				// console.log('close');
			},
			yzmdl(url,val){
				uni.navigateTo({
					url: url+val
				})
			},
			jumper(url) {
				uni.navigateTo({
					url: url
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.touming {
		opacity: 0.5;
	}

	.sjyz {
		width: 44rpx;
		height: 44rpx;
	}

	.denglhbtn {
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #FFFFFF;
		width: 590rpx;
		height: 80rpx;
		background: #2361FF;
		border: 1rpx solid #2361FF;
		border-radius: 52rpx;
	}

	.denglhbtns {
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: 800;
		color: #2361FF;
		width: 590rpx;
		height: 80rpx;
		background: #fff;
		border: 1rpx solid #2361FF;
		border-radius: 52rpx;
	}

	.logbox {
		padding: 180rpx 80rpx 30rpx;
	}
</style>